import { parse } from 'url';

// mock tableListDataSource
const tableListDataSource = [
  {
    desc: "0.2",
    name: "1",
    jiliang: "20",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 1,
  },
  {
    desc: "0.4",
    name: "2",
    jiliang: "40",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 1,
  },
  {
    desc: "0.8",
    name: "3",
    jiliang: "80",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 1,
  },
  {
    desc: "0.2",
    name: "4",
    jiliang: "200",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 2,
  },
  {
    desc: "0.4",
    name: "5",
    jiliang: "400",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 2,
  },
  {
    desc: "0.8",
    name: "6",
    jiliang: "800",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 2,
  },
  {
    desc: "0.2",
    name: "7",
    jiliang: "2000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 3,
  },
  {
    desc: "0.4",
    name: "8",
    jiliang: "4000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 3,
  },
  {
    desc: "0.8",
    name: "9",
    jiliang: "8000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 3,
  },
  {
    desc: "0.1",
    name: "10",
    jiliang: "10000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 4,
  },
  {
    desc: "0.2",
    name: "11",
    jiliang: "20000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 4,
  },
  {
    desc: "0.4",
    name: "12",
    jiliang: "40000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 4,
  },
  {
    desc: "0.6",
    name: "13",
    jiliang: "60000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 4,
  },
  {
    desc: "0.8",
    name: "14",
    jiliang: "90000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 4,
  },
  {
    desc: "1.0",
    name: "15",
    jiliang: "100000",
    updatedAt: "2019-04-12T14:00:00.000Z",
    pingHao: 4,
  },
  
  

];
// for (let i = 0; i < 46; i += 1) {
//   tableListDataSource.push({
//     key: i,
//     disabled: i % 6 === 0,
//     href: 'https://ant.design',
//     avatar: [
//       'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
//       'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
//     ][i % 2],
//     name: `TradeCode ${i}`,
//     title: `一个任务名称 ${i}`,
//     owner: '曲丽丽',
//     desc: '这是一段描述',
//     callNo: Math.floor(Math.random() * 1000),
//     status: Math.floor(Math.random() * 10) % 4,
//     updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     progress: Math.ceil(Math.random() * 100),
//   });
// }

function getTuoMinZhen(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  return res.json(result);
}

export default {
  'GET /api/tuominzhen': getTuoMinZhen,
};
